# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=labplot
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.12.0
pkgrel=1
pkgdesc="FREE, open source and cross-platform Data Visualization and Analysis software accessible to everyone. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://labplot.org'
msys2_repository_url="https://invent.kde.org/education/labplot"
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-breeze"
         "${MINGW_PACKAGE_PREFIX}-cfitsio"
         "${MINGW_PACKAGE_PREFIX}-dbc_parser_cpp"
         "${MINGW_PACKAGE_PREFIX}-discount"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-ixion"
         "${MINGW_PACKAGE_PREFIX}-karchive"
         "${MINGW_PACKAGE_PREFIX}-kcompletion"
         "${MINGW_PACKAGE_PREFIX}-kconfig"
         "${MINGW_PACKAGE_PREFIX}-kconfigwidgets"
         "${MINGW_PACKAGE_PREFIX}-kcoreaddons"
         "${MINGW_PACKAGE_PREFIX}-kcrash"
         "${MINGW_PACKAGE_PREFIX}-ki18n"
         "${MINGW_PACKAGE_PREFIX}-kiconthemes"
         "${MINGW_PACKAGE_PREFIX}-kio"
         "${MINGW_PACKAGE_PREFIX}-kparts"
         "${MINGW_PACKAGE_PREFIX}-kservice"
         "${MINGW_PACKAGE_PREFIX}-ktextwidgets"
         "${MINGW_PACKAGE_PREFIX}-kuserfeedback"
         "${MINGW_PACKAGE_PREFIX}-kwidgetsaddons"
         "${MINGW_PACKAGE_PREFIX}-kxmlgui"
         "${MINGW_PACKAGE_PREFIX}-libcerf"
         "${MINGW_PACKAGE_PREFIX}-liborigin"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-matio"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-orcus"
         "${MINGW_PACKAGE_PREFIX}-poppler-qt6"
         "${MINGW_PACKAGE_PREFIX}-purpose"
         "${MINGW_PACKAGE_PREFIX}-qt-advanced-docking-system"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-mqtt"
         "${MINGW_PACKAGE_PREFIX}-qt6-serialport"
         "${MINGW_PACKAGE_PREFIX}-qt6-svg"
         "${MINGW_PACKAGE_PREFIX}-qxlsx"
         "${MINGW_PACKAGE_PREFIX}-readstat"
         "${MINGW_PACKAGE_PREFIX}-syntax-highlighting"
         "${MINGW_PACKAGE_PREFIX}-vector_blf"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-icoutils"
             "${MINGW_PACKAGE_PREFIX}-extra-cmake-modules"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-kdoctools")
             "${MINGW_PACKAGE_PREFIX}-knewstuff"
             "${MINGW_PACKAGE_PREFIX}-shared-mime-info")
source=("https://download.kde.org/stable/labplot/labplot-${pkgver}.tar.xz"
        "001-fix-linking-with-lld.patch"
        "003-fix-build-with-qxlsx.patch"
        "005-fix-find-qtadvanceddocking.patch")
sha256sums=('25f1751261ec2d11312f0179049ea1f16db33b3a89800dc572d68ad8d267afe7'
            '89df5a4a7996b7b9cc4fe65c85b89f46f1ee82c8058a71d9ddf065233122bcbb'
            'f5230ece1292bf0d400f1271034111936952e00ae1fd7ed817fd4e89a7af1ae3'
            '97c5e4e5f6de529d63448232aa0ed982e21868b9359357dba230bc2a0bb976a8')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${pkgver}

  apply_patch_with_msg \
    001-fix-linking-with-lld.patch \
    003-fix-build-with-qxlsx.patch \
    005-fix-find-qtadvanceddocking.patch

  # remove conflict with breeze package
  rm data/color-schemes/BreezeDark.colors

  # clang picked it as <format> header file
  rm src/3rdparty/liborigin/FORMAT
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DENABLE_CANTOR=OFF \
      -DENABLE_DISCOUNT=ON \
      -DENABLE_EIGEN3=ON \
      -DENABLE_FFTW=ON \
      -DENABLE_FITS=ON \
      -DENABLE_HDF5=ON \
      -DENABLE_LIBCERF=ON \
      -DENABLE_LIBORIGIN=ON \
      -DENABLE_MATIO=ON \
      -DENABLE_MCAP=ON \
      -DENABLE_MQTT=ON \
      -DENABLE_NETCDF=ON \
      -DENABLE_ORCUS=ON \
      -DENABLE_QTSERIALPORT=ON \
      -DENABLE_QTSVG=ON \
      -DENABLE_ROOT=ON \
      -DENABLE_XLSX=ON \
      -DENABLE_TESTS=OFF \
      -DENABLE_SDK=OFF \
      -DLOCAL_DBC_PARSER=OFF \
      -DLOCAL_VECTOR_BLF=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSES/* \
    -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/
}
